package behaviorPattern.strategy;

/**
 * @Author: Alex Hu
 * Date: 2020/2/22 2:31 下午
 * Content:
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.Serial;

/**
 * 抽象策略类：大闸蟹加工类
 */
interface CrabCooking {
    /**
     * 做菜方法
     */
    void cookingMethod();
}

public class CrabCookingStrategy implements ItemListener {
    private JFrame f;
    private JRadioButton qz, hs;
    private JPanel CenterJP, SouthJP;
    /**
     * 厨房
     */
    private Kitchen cf;
    /**
     * 大闸蟹加工者
     */
    private CrabCooking qzx, hsx;

    CrabCookingStrategy() {
        f = new JFrame("策略模式在大闸蟹做菜中的应用");
        f.setBounds(100, 100, 500, 400);
        f.setVisible(true);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SouthJP = new JPanel();
        CenterJP = new JPanel();
        f.add("South", SouthJP);
        f.add("Center", CenterJP);
        qz = new JRadioButton("清蒸大闸蟹");
        hs = new JRadioButton("红烧大闸蟹");
        qz.addItemListener(this);
        hs.addItemListener(this);
        ButtonGroup group = new ButtonGroup();
        group.add(qz);
        group.add(hs);
        SouthJP.add(qz);
        SouthJP.add(hs);

        /**厨房*/
        cf = new Kitchen();
        /**清蒸大闸蟹类*/
        qzx = new SteamedCrabs();
        /**红烧大闸蟹类*/
        hsx = new BraisedCrabs();
    }

    public static void main(String[] args) {
        new CrabCookingStrategy();
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        JRadioButton jc = (JRadioButton) e.getSource();
        if (jc == qz) {
            cf.setStrategy(qzx);
            /**清蒸*/
            cf.CookingMethod();
        } else if (jc == hs) {
            cf.setStrategy(hsx);
            /**红烧*/
            cf.CookingMethod();
        }
        CenterJP.removeAll();
        CenterJP.repaint();
        CenterJP.add((Component) cf.getStrategy());
        f.setVisible(true);
    }
}

/**
 * 具体策略类：清蒸大闸蟹
 */
class SteamedCrabs extends JLabel implements CrabCooking {
    private static final long serialVersionUID = 1L;

    @Override
    public void cookingMethod() {
        this.setIcon(new ImageIcon("src/main/java/behaviorPattern/strategy/SteamedCrabs.jpg"));
        this.setHorizontalAlignment(CENTER);
    }
}

/**
 * 具体策略类：红烧大闸蟹
 */
class BraisedCrabs extends JLabel implements CrabCooking {
    @Serial
    private static final long serialVersionUID = 1L;

    @Override
    public void cookingMethod() {
        this.setIcon(new ImageIcon("src/main/java/behaviorPattern/strategy/BraisedCrabs.jpg"));
        this.setHorizontalAlignment(CENTER);
    }
}

/**
 * 环境类：厨房
 */
class Kitchen {
    /**
     * 抽象策略
     */
    private CrabCooking strategy;

    public CrabCooking getStrategy() {

        return strategy;
    }

    public void setStrategy(CrabCooking strategy) {

        this.strategy = strategy;
    }

    public void CookingMethod() {

        /**做菜*/
        strategy.cookingMethod();
    }
}
